﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace banolo12 {
	public partial class MainWindow : Form {
		Graphics gr;
		BackgroundWorker backgroundMusic = new BackgroundWorker();
		BackgroundWorker levelUpBackground = new BackgroundWorker();
		BackgroundWorker displayWorker = new BackgroundWorker();
		bool stopMusic = false;
		int score = 0;
		bool gameOver = false;
		bool stop = false;
		
		public MainWindow() {
			InitializeComponent();
			backgroundMusic.WorkerSupportsCancellation = true;
			levelUpBackground.WorkerSupportsCancellation = true;
			this.ResizeRedraw = true;

			backgroundMusic.DoWork += (s, e) => {
				while (!stopMusic) {
					TrackPlayer.SetDuration(200);
					TrackPlayer.Play(Tracks.mainTrack, true);
				}
			};

			SetUpBitmaps();
		}

		private void OnLoad(object sender, EventArgs e) {
			backgroundMusic.RunWorkerAsync();
		}

		private void LevelUp() {
			backgroundMusic.CancelAsync();
			levelUpBackground.RunWorkerAsync();
		}

		private void SetUpBitmaps() {
			Bitmap manolo12 = Properties.Resources.manolo12;
			manolo12.MakeTransparent(Color.Magenta);
			manolo.Image = manolo12;
		}

		protected override void OnClick(EventArgs e) {
			base.OnClick(e);
		}

		private void OnPaint(object sender, PaintEventArgs e) {
			
		}

		private void SendBan() {
			PictureBox banPicture = new PictureBox();
			banPicture.Size = new Size(8, 8);
			banPicture.Image = Properties.Resources.ban;
			banPicture.Name = "ban";
			banPicture.Location = manolo.Location;
			banPicture.BringToFront();
			banPicture.Top -= 6;
			banPicture.Left += 12;
			this.Controls.Add(banPicture);
		}

		private void MainWindow_KeyDown(object sender, KeyEventArgs e) {
			switch (e.KeyCode) {
				case Keys.A:
					if (manolo.Left > 0)
						manolo.Left -= 8;
					break;

				case Keys.D:
					if (manolo.Right < this.Width - manolo.Width)
						manolo.Left += 8;
					break;

				case Keys.W:
					SendBan();
					break;

				case Keys.Escape:
					if (!stop) {
						stop = true;
						banTimer.Stop();
						entitySpawner.Stop();
						banTimer.Enabled = false;
						entitySpawner.Enabled = false;
						TrackPlayer.SetVolume(0);
					} else {
						stop = false;
						banTimer.Enabled = true;
						entitySpawner.Enabled = true;
						banTimer.Start();
						entitySpawner.Start();
						TrackPlayer.SetVolume(200);
					}

					break;

				default:
					break;
			}
		}

		private Bitmap charToImg(char c) {
			switch (c) {
				case '1':
					return Properties.Resources.uno;

				case '2':
					return Properties.Resources.dos;

				case '3':
					return Properties.Resources.tres;

				case '4':
					return Properties.Resources.cuatro;

				case '5':
					return Properties.Resources.cinco;

				case '6':
					return Properties.Resources.seis;

				case '7':
					return Properties.Resources.siete;

				case '8':
					return Properties.Resources.ocho;

				case '9':
					return Properties.Resources.nueve;

				default:
					return Properties.Resources.cero;
			}
		}

		private void GameOver() {
			banTimer.Stop();
			entitySpawner.Stop();
			banTimer.Enabled = false;
			entitySpawner.Enabled = false;
			gameOver = true;
			stopMusic = true;
			backgroundMusic.CancelAsync();
			TrackPlayer.StopTrackLoop();
			TrackPlayer.SetVolume(0);
			Console.Beep(500, 100);
			Console.Beep(300, 200);
			Console.Beep(100, 300);
			PictureBox pb = new PictureBox();
			pb.Image = Properties.Resources.gameover;
			pb.SizeMode = PictureBoxSizeMode.StretchImage;
			pb.Dock = DockStyle.Fill;
			pb.Click += (s, e) => {
				Application.Restart();
			};
			this.Controls.Add(pb);
			pb.BringToFront();
		}

		private void banTimer_Tick(object sender, EventArgs e) {
			if (!gameOver) {
				if (score >= 0) {
					string scoreString = score.ToString().PadLeft(5, '0');
					panel4.BackgroundImage = charToImg(scoreString[0]);
					panel5.BackgroundImage = charToImg(scoreString[1]);
					panel6.BackgroundImage = charToImg(scoreString[2]);
					panel7.BackgroundImage = charToImg(scoreString[3]);
					panel8.BackgroundImage = charToImg(scoreString[4]);
				} else {
					GameOver();
				}

				foreach (PictureBox banBox in this.Controls.Find("ban", true)) {
					foreach (PictureBox entity in this.Controls.Find("entity", true)) {
						if (banBox.Bounds.IntersectsWith(entity.Bounds)) {
							if (entity.Tag == "joselo") {
								this.Controls.Remove(banBox);
								this.Controls.Remove(entity);
								score -= 6;
								Console.Beep(500, 100);
								Console.Beep(300, 200);
							} else {
								this.Controls.Remove(banBox);
								this.Controls.Remove(entity);
								score += 1;
								Console.Beep(1000, 10);
								Console.Beep(1000, 50);
							}
						}
					}

					if (banBox.Top > 0)
						banBox.Top -= 1;
					else this.Controls.Remove(banBox);
				}

				foreach (PictureBox entity in this.Controls.Find("entity", true)) {
					if (entity.Top < this.Height - 32)
						entity.Top++;
					else {
						this.Controls.Remove(entity);
						if (entity.Tag != "joselo") {
							Console.Beep(500, 100);
							Console.Beep(300, 200);
							score -= 2;
						} else {
							Console.Beep(1000, 10);
							Console.Beep(1000, 50);
						}
					}
				}
			}
		}

		private static Bitmap randToBad() {
			switch (new Random().Next(0, 3)) {
				case 0:
					return Properties.Resources.sxs;
				
				case 1:
					return Properties.Resources.rxr;

				default:
					return Properties.Resources.premium;
			}
		}

		private void entitySpawner_Tick(object sender, EventArgs e) {
			if (!gameOver) {
				switch (new Random().Next(0, 4)) {
					case 0:
						PictureBox entity = new PictureBox();
						Bitmap joselo = Properties.Resources.joselo;

						joselo.MakeTransparent(Color.Magenta);
						entity.BackgroundImage = (new Random().Next(0, 2) == 0) ? Properties.Resources.pelisgratis : null;
						entity.BackgroundImageLayout = ImageLayout.None;
						entity.Size = new Size(39, 40);
						entity.SizeMode = PictureBoxSizeMode.CenterImage;
						entity.Image = joselo;
						entity.Name = "entity";
						entity.Tag = "joselo";
						entity.Location = new Point(new Random().Next(0, this.Width - 32), 12);
						this.Controls.Add(entity);
						break;

					default:
						entity = new PictureBox();
						Bitmap troll = Properties.Resources.troll;

						troll.MakeTransparent(Color.Magenta);
						entity.BackgroundImage = randToBad();
						entity.BackgroundImageLayout = ImageLayout.None;
						entity.Size = new Size(32, 32);
						entity.SizeMode = PictureBoxSizeMode.CenterImage;
						entity.Image = troll;
						entity.Name = "entity";
						entity.Tag = "troll";
						entity.Location = new Point(new Random().Next(0, this.Width - 32), 12);
						this.Controls.Add(entity);
						break;
				}
			}
		}
	}
}
